package com.lmh.ymw.controller.news;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.ymw.entity.News;
import com.lmh.ymw.entity.RespBean;
import com.lmh.ymw.service.news.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("news")
@CrossOrigin
public class NewsController {
    @Autowired
    NewsService service;
    @PostMapping("/")
    public RespBean add(News u){
        boolean add = service.addNews(u);
        if (add ==true) {
            return RespBean.ok("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }


    @PutMapping("/")
    public RespBean up(News news){
        boolean update = service.update(news);
        if (update ==true) {
            return RespBean.ok("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }
    @GetMapping("/list")
    public RespBean list(@RequestParam(required = false) Integer pageNum ){
        PageHelper.startPage(pageNum,4);
        List<News> newsList = service.queryNewsList();
        PageInfo<News> list=new PageInfo<News>(newsList);
        return RespBean.ok("查询成功",list);
    }
    @GetMapping("/{id}")
    public RespBean getNews(@PathVariable Integer id){
        News newsById = service.findNewsById(id);
        return RespBean.ok("查询成功",newsById);
    }
    @DeleteMapping("/{id}")
    public RespBean del(@PathVariable Integer id){
        boolean b = service.deleteNews(id);
        if (b ==true) {
            return RespBean.ok("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }



}
